<template>
  <div class="gather" :class="{ padbottom: !isCheck }">
    <div v-if="gatherData.length > 0">
      <!-- <van-checkbox-group v-model="result"> -->
        <div v-for="(item, index) in gatherData" :key="index" class="module">
          <van-cell-group>
            <!-- <van-cell class="choose">
              <van-checkbox
                shape="square"
                icon-size="14px"
                checked-color="#009966"
                :name="index"
              ></van-checkbox>
            </van-cell> -->
            <van-field
              v-model="item.involve_plate"
              rows="1"
              autosize
              label-class="labelColor"
              input-align="right"
              label="涉及板块"
              label-width="4em"
              placeholder="涉及板块"
              type="textarea"
              readonly
            />
            <van-field
              v-model="item.involve_office"
              label="涉及处室"
              label-width="4em"
              input-align="right"
              label-class="labelColor"
              placeholder="涉及处室"
              readonly
            />
            <van-field
              v-model="item.involve_system"
              label="涉及系统"
              label-width="4em"
              input-align="right"
              label-class="labelColor"
              placeholder="涉及系统"
              readonly
            />
            <van-field
              v-model="item.involve_content"
              rows="1"
              autosize
              label-class="labelColor"
              input-align="right"
              label="涉及内容"
              label-width="4em"
              placeholder="涉及内容"
              type="textarea"
              readonly
            />
            <van-field
              v-model="item.source_type"
              label="资源类型"
              input-align="right"
              label-width="4em"
              label-class="labelColor"
              placeholder="资源类型"
              readonly
            />
            <van-field
              v-model="item.workflow"
              label-width="8em"
              label="预计实施周期(月)"
              input-align="right"
              label-class="labelColor"
              placeholder="4"
              :readonly="isCheck"
            />
            <van-field
              v-model="item.it"
              label-width="8em"
              label="预计IT投入(万元)"
              input-align="right"
              label-class="labelColor"
              placeholder="1,400"
              :readonly="isCheck"
            />
          </van-cell-group>
        </div>
      <!-- </van-checkbox-group> -->
    </div>

    <div class="module">
      <van-cell-group>
        <van-field
          v-if="isGatherNode"
          v-model="detailData.development_leader_team"
          label="开发牵头团队"
          is-link
          readonly
          arrow-direction="down"
          label-class="labelColor"
          @click="choose(1)"
          input-align="right"
          placeholder="请选择开发牵头团队"
        />
        <van-field
          v-else
          v-model="detailData.development_leader_team"
          label="开发牵头团队"
          label-width="6em"
          input-align="right"
          label-class="labelColor"
          placeholder="开发牵头团队"
          readonly
        />
        <van-field
          v-if="isGatherNode"
          v-model="detailData.project_leader_name"
          label="项目负责人"
          is-link
          readonly
          arrow-direction="down"
          label-class="labelColor"
          @click="choose(0)"
          input-align="right"
          placeholder="请选择项目负责人"
        />
        <van-field
          v-else
          v-model="detailData.project_leader_name"
          label="项目负责人"
          label-width="5em"
          input-align="right"
          label-class="labelColor"
          placeholder="项目负责人"
          readonly
        />
        <van-field
          v-model="detailData.involve_team"
          label="涉及团队"
          input-align="right"
          label-class="labelColor"
          placeholder="涉及团队"
          readonly
        />
        <van-field
          v-model="detailData.system_leader"
          label="系统负责人"
          input-align="right"
          label-class="labelColor"
          placeholder="系统负责人"
          readonly
        />
        <van-field
          v-model="detailData.estimate_it_amount"
          label="预计it投入(万元)"
          label-width="8em"
          input-align="right"
          label-class="labelColor"
          placeholder="预计it投入金额"
          readonly
        />
        <van-field
          v-model="detailData.project_amount"
          label="立项金额(万元)"
          label-width="8em"
          input-align="right"
          label-class="labelColor"
          placeholder="立项金额"
          readonly
        />
      </van-cell-group>
    </div>
    <van-popup position="bottom" get-container="body" v-model="showPop">
      <van-picker
        show-toolbar
        :columns="columns"
        @confirm="onConfirm"
        @cancel="showPop = false"
      />
    </van-popup>
    <div v-if="isCurrentNode" class="form-btn">
      <div class="btn btn-color1" @click="handleMoreOpera">更多操作</div>
      <!-- <div class="btn btn-color2">保存</div> -->
      <div class="btn btn-color3" @click="submit">提交</div>
    </div>
    <van-action-sheet
      v-model="showMoreOpera"
      :actions="actions"
      cancel-text="取消"
      close-on-click-action
      @select="onSelect"
      @cancel="onCancel"
    />
  </div>
</template>

<script>
import {
  serviceRequirement,
  requirementDetails,
  hzprojectLeader,
  leaderTeam,
  detailsEdit,
  getRequirementRelated,
  analysisInit,
  harvestInit,
} from "@/api/service.js";
import { Toast } from "vant";
export default {
  props: ["id", "currentNode"], //服务单id
  data() {
    return {
      gatherData: [],
      text: "13123123",
      actions: [{ name: "OA流程" }, { name: "审批记录" }],
      showMoreOpera: false,
      isCheck: false,
      showPop: false,
      projeLeader: "", //项目负责人
      projectTeam: "", //开发牵头团队
      type: 0, //下拉类型
      detailData: "", //详情数据
      columns: "",
      isCurrentNode: true,
      isGatherNode: true,
      leaderTeamId: "", //开发牵头团队ID
      projectLeaderId: "", //项目负责人id
      systemLeaderid: "", //系统负责人ID
      relatedData: "", //开发牵头团队关联数据
      kfqttd: "", // default开发牵头团队
      xmfzr: "", // default项目负责人
      changed: 0, // 是否修改过  0没有  1有
      result: [],
    };
  },
  mounted() {
    if (this.id) {
      this.getGatherData();
      this.checkNode();
    }
  },
  methods: {
    handleMoreOpera() {
      this.showMoreOpera = true;
    },
    onCancel() {
      this.showMoreOpera = false;
    },
    //检查是否是当前节点
    checkNode() {
      console.log(this.currentNode);
      if (
        this.currentNode == "T10017" ||
        this.currentNode == "T10018" ||
        this.currentNode == "T10086" ||
        this.currentNode == "T10082"
      ) {
        this.isCurrentNode = true;
      } else {
        this.isCurrentNode = false;
      }
      if (this.currentNode == "T10017") {
        this.isGatherNode = true;
      } else {
        this.isGatherNode = false;
      }
    },
    submit() {
      if (this.currentNode == "T10017") {
        analysisInit({
          server_id: this.id,
        }).then((res) => {
          if (res.code != 200) {
            Toast(res.message);
          }
        });
        harvestInit({
          server_id: this.id,
        }).then((res) => {
          if (res.code != 200) {
            Toast(res.message);
          }
        });
      }
      if (
        this.kfqttd == this.detailData.development_leader_team &&
        this.xmfzr == this.detailData.project_leader_name
      ) {
        this.changed = 0;
      } else {
        this.changed = 1;
      }
      console.log(this.projectLeaderId);
      console.log(this.result)
      let sourceType = [];
      let sourceTypeId = [];
      for(let i=0;i<this.gatherData.length;i++){
        sourceType.push(this.gatherData[i].source_type)
        sourceTypeId.push(this.gatherData[i].source_type_id)
      }
      detailsEdit({
        server_id: this.id,
        project_leader: this.detailData.project_leader_name,
        project_leader_id: this.projectLeaderId,
        development_leader_team: this.detailData.development_leader_team,
        development_leader_team_id: this.leaderTeamId,
        system_name_id: this.relatedData.system_name_id,
        system_name: this.relatedData.system_name,
        leader_id: this.relatedData.leader_id,
        leader: this.relatedData.leader,
        text76: this.detailData.estimate_it_amount,
        projectAmount: this.detailData.project_amount,
        changed: this.changed,
        node_id: this.currentNode,
      }).then((res) => {
        if (res.code == 200) {
          this.$router.push({
            path: "/service/node-flow",
            query: {
              id: this.id,
              showUser: false,
              source: this.currentNode,
              sourceType:sourceType,
              sourceTypeId:sourceTypeId,
              chooseType : this.result
            },
          });
        } else {
          Toast(res.message);
        }
      });
    },
    onSelect(active, index) {
      console.log(active, index);
      if (active.name == "审批记录") {
        this.$router.push({
          path: "/service/approvalRecord",
          query: {
            id: this.id,
          },
        });
      } else if (active.name == "OA流程") {
        this.$router.push({
          path: "/service/oaFlow",
          query: {
            id: this.id,
          },
        });
      }
    },
    //下拉
    choose(type) {
      this.columns = [];
      if (type == 0) {
        this.getLeader();
      } else {
        this.getTeam();
      }
      this.type = type;
      this.showPop = true;
    },
    getLeader() {
      hzprojectLeader({
        server_id: this.id,
        lead_team_id: this.leaderTeamId,
        node_id: this.currentNode,
      }).then((res) => {
        if (res.code == 200) {
          this.projeLeader = res.data;
          for (var i = 0; i < res.data.length; i++) {
            this.columns.push(res.data[i].name);
          }
        }
      });
    },
    getTeam() {
      leaderTeam({
        server_id: this.id,
        node_id: this.currentNode,
      }).then((res) => {
        if (res.code == 200) {
          this.projectTeam = res.data;
          for (var i = 0; i < res.data.length; i++) {
            this.columns.push(res.data[i].name);
          }
        }
      });
    },
    onConfirm(value, index) {
      console.log(value);
      if (this.type == 0) {
        this.detailData.project_leader_name = value;
        this.projectLeaderId = this.projeLeader[index].id;
      } else {
        this.detailData.development_leader_team = value;
        this.leaderTeamId = this.projectTeam[index].id;
        hzprojectLeader({
          server_id: this.id,
          lead_team_id: this.leaderTeamId,
          node_id: this.currentNode,
        }).then((res) => {
          if (res.code == 200) {
            this.projeLeader = res.data;
            this.detailData.project_leader_name =
              res.data.length > 0 ? res.data[0].name : "";
            this.projectLeaderId = res.data.length > 0 ? res.data[0].id : "";
          }
        });
        getRequirementRelated({
          server_id: this.id,
          lead_team_id: this.projectTeam[index].id,
          node_id: this.currentNode,
        }).then((res) => {
          console.log(res);
          if (res.code == 200) {
            this.relatedData = res.data[0];
            this.detailData.involve_team = res.data[0].system_name;
            this.detailData.system_leader = res.data[0].leader;
          }
        });
      }
      this.showPop = false;
    },
    getGatherData() {
      serviceRequirement({
        server_id: this.id,
        node_id: this.currentNode,
      }).then((res) => {
        console.log(res);
        if (res.code == 200) {
          this.gatherData = res.data;
        }
      });
      requirementDetails({
        server_id: this.id,
        node_id: this.currentNode,
      }).then((res) => {
        console.log(res);
        if (res.code == 200) {
          this.detailData = res.data;
          this.projectLeaderId = res.data.project_leader;
          this.leaderTeamId = res.data.development_leader_team_id;
          this.kfqttd = this.detailData.development_leader_team;
          this.xmfzr = this.detailData.project_leader_name;
        }
      });
    },
  },
};
</script>


<style lang="less" scoped>
.gather.padbottom {
  padding-bottom: 100px;
}
.module {
  margin-top: 12px;
}
/deep/.labelColor {
  color: rgba(49, 53, 65, 0.6);
}
/deep/.van-field__control {
  color: #313541;
}
.choose {
  padding: 9px 0;
  width: calc(100% - 32px);
  margin-left: 16px;
  border-bottom: 1px solid #eceef4;
}
.form-btn {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  background: #fff;
  height: 34px;
  padding: 13px 15px;
  padding-bottom: calc(13px + env(safe-area-inset-bottom));
  .btn {
    flex: 1;
    height: 34px;
    line-height: 34px;
    text-align: center;
    font-size: 14px;
    margin-right: 6px;
    border-radius: 5px;
    &:last-child {
      margin-right: 0;
    }
  }
  .btn-color1 {
    background: rgba(147, 157, 173, 0.18);
    color: #313541;
  }
  .btn-color3 {
    background: #009966;
    color: #fff;
  }
}
</style>